package org.jeecg.modules.parameter.biz.controlltr;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.parameter.api.dto.power.SimPowerDto;
import org.jeecg.modules.parameter.api.dto.power.SimPowerUpdateDto;
import org.jeecg.modules.parameter.api.dto.power.SimPowerWithDto;
import org.jeecg.modules.parameter.api.dto.power.SimPowerWithUpdateDto;
import org.jeecg.modules.parameter.api.entity.power.SimPower;
import org.jeecg.modules.parameter.api.entity.power.SimPowerWith;
import org.jeecg.modules.parameter.api.vo.power.SimPowerJsonVo;
import org.jeecg.modules.parameter.biz.service.SimLinePowerService;
import org.jeecg.modules.parameter.biz.service.SimLinePowerWithService;
import org.jeecg.modules.parameter.common.api.dto.PatchPO;
import org.jeecg.modules.parameter.common.utils.oConvertUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lsy
 * @date 2024/09/17
 */
@Api(tags = "参数管理-供电区间管理")
@RestController
@RequestMapping(value = {"/linePowerInfo"}, produces = {"application/json; charset=utf-8"})
@Slf4j
public class SimLinePowerController extends JeecgController<SimPower, SimLinePowerService> {
    @Resource
    private SimLinePowerService linePowerService;

    @Resource
    private SimLinePowerWithService linePowerWithService;

    @ApiOperation(value = "按线路ID查询供电区间信息", notes = "按照线路ID查询供电区间信息")
    @PostMapping(value = "/getAllPower")
    public Result<List<SimPowerJsonVo>> getAllPower(@RequestParam(name = "lineId", required = true) String id) {
        return Result.ok(linePowerService.getAllPowerWith(id));
    }

    @ApiOperation(value = "按ID查询供电区间信息", notes = "按ID查询供电区间信息")
    @PostMapping(value = "/getPowerById")
    public Result<SimPowerJsonVo> getPowerById(@RequestParam(name = "id", required = true) String id) {
        return Result.ok(linePowerService.getPowerByPowerId(id));
    }

    @ApiOperation(value = "添加供电区间信息", notes = "查询辅助设施")
    @PostMapping(value = "/addPower")
    public Result<SimPower> addPower(@RequestBody SimPowerDto simPowerDto) {
        SimPower simPower = oConvertUtils.entityToModel(simPowerDto, SimPower.class);

        linePowerService.save(simPower);
        List<SimPowerWith> simPowerWith = new ArrayList<>();
        for (String forStationId : simPowerDto.getPowerWith()) {
            SimPowerWith temp = new SimPowerWith();
            temp.setForStationId(forStationId);
            assert simPower != null;
            temp.setMainPowerId(simPower.getId());
            simPowerWith.add(temp);
        }
        linePowerWithService.insertPowerWith(simPowerWith);

        return Result.ok("添加成功");
    }

    @ApiOperation(value = "修改供电区间信息", notes = "查询辅助设施")
    @PutMapping(value = "/updatePower")
    public Result<SimPower> updatePower(@RequestBody SimPowerUpdateDto patchPO) {


        PatchPO patchTemp = patchPO.getPatchPO();
        if (patchTemp != null) {
            linePowerService.updatePatch(patchTemp);
        }

        SimPowerWithUpdateDto powerWith = patchPO.getPowerWith();
        if (powerWith != null) {
            SimPowerWithDto powerWithTemp = new SimPowerWithDto();
            linePowerWithService.delPowerWith(powerWith.getId());
            powerWithTemp.setMainPowerId(powerWith.getId());
            powerWith.getPowerWith().forEach(s -> {
                powerWithTemp.setForStationId(s);
                linePowerWithService.save(oConvertUtils.entityToModel(powerWithTemp, SimPowerWith.class));
            });
        }

        return Result.ok("更新成功");
    }

    @ApiOperation(value = "按ID删除供电区间信息", notes = "查询辅助设施")
    @DeleteMapping(value = "/delPower")
    public Result<SimPower> delPower(@RequestParam(name = "id", required = true) String id) {
        linePowerService.removeById(id);
        linePowerWithService.delPowerWith(id);
        return Result.ok("删除成功");
    }
}
